//
//  HMStatusViewModel.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/24.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMStatusViewModel.h"
#import "HMWeiBoOC-Swift.h" 
//#import "NSDate+NSDate_Extension.h"



@implementation HMStatusViewModel


-(instancetype)initWithStatus:(HMStatus *)status{
    if (self = [super init]) {
        self.status = status;
    }
    return self;
}

-(void)setStatus:(HMStatus *)status{
    _status = status;//缺失  会导致数据为空.
    //处理业务逻辑
    [self handleMBrankImage:status.user.mbrank];
   
    [self handleVerifiedTypeImage:status.user.verified_type];
    
    [self handleSourceContent:status.source];
    
    self.retweetCountContent = [self handleCountContent:status.reposts_count title:@"转发"];
    self.commentCountContent = [self handleCountContent:status.comments_count title:@"评论"];
    self.unlikeCountContent = [self handleCountContent:status.attitudes_count title:@"赞"];
    
    [self handleRetweetContent:status];
    
}

//-(void)setTimeContent:(NSString *)timeContent{//计算属性?
////    fnt = [[NSDate sinaDate:self.status.created_at] sinaDateString];
////    _timeContent = timeContent;
////    NSDate *date = [NSDate date];
//    
////    [date sinaDate(createAt: timeContent)];// ??? OC对象 无法调用swift中的对象方法 类方法.
//   
//}
-(NSString *)timeContent{//计算属性?  即get方法.
    
    
    if ([self.status.created_at isKindOfClass:[NSString class]]) {
        //NSDate+Extension.swift 可以用
       return [NSDate sinaDateWithCreateAt:self.status.created_at].sinaDateString;
//        return [[NSDate sinaDate:self.status.created_at] sinaDateString];
    }
    return nil;
}


//处理来源数据
-(void)handleSourceContent:(NSString *)source{
    if ([source containsString:@"\>"] && [source containsString:@"</"]) {
//        NSLog(@"%@",source);
        NSRange startRange = [source rangeOfString:@"\>"];
        NSRange endRange = [source rangeOfString:@"</"];
        self.sourceContent = [source substringWithRange:NSMakeRange(startRange.location + 1, endRange.location - startRange.location-1)];
        
    }
}

//处理认证类型等级图片逻辑
-(void)handleVerifiedTypeImage:(int)verifiedType{
    switch (verifiedType) {
        case 1:
            self.verifiedTypeImage = [UIImage imageNamed:@"avatar_vip"];
            break;
        case 2:
            self.verifiedTypeImage = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 3:
            self.verifiedTypeImage = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 5:
            self.verifiedTypeImage = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 220:
            self.verifiedTypeImage = [UIImage imageNamed:@"avatar_grassroot"];
            break;
            
        default:
            self.verifiedTypeImage = [UIImage imageNamed:@"avatar_vip"];
            break;
    }
}


//  处理转发微博内容显示
-(void)handleRetweetContent:(HMStatus *)status{
    
    if (status.retweeted_status != nil) {
        NSString *name = status.retweeted_status.user.screen_name;
        
        NSString *text = status.retweeted_status.text;
        
        if (name && text) {
            self.retweetContent = [NSString stringWithFormat:@"#%@:%@!",name,text];
        }
    }
}

 //处理评论,转发,赞  数据显示
-(NSString *)handleCountContent:(int)count title:(NSString *)title{

    if (count > 0) {
        return @(count).description;
    }else{
        return title;
    }
}


//  处理会员等级逻辑
-(void)handleMBrankImage:(int)mbRank{
    if (mbRank >= 1 && mbRank <= 6) {
        self.mbrankImage = [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%zd",mbRank]];
    }
}

@end
